using System;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Pad = Microsoft.Xna.Framework.Input.GamePad;

namespace S2Engine.Input.Xbox360
{
    /// <summary>
    /// Trata das entradas provinientes do Controle do Xbox 360 e seu Headset
    /// </summary>
    public class GamePad
    {
        #region Attributes
        private PlayerIndex playerIndex;
        private GamePadState state;
        private GamePadCapabilities caps;
        private bool isconnected;
       // private GamePad gamepad;
        #endregion

        #region Events
        public event EventHandler ControllerConnected;
        public event EventHandler ControllerDisconnected;

        protected void OnControllerConnected(EventArgs e)
        {
            if (ControllerConnected != null)
                ControllerConnected(this, e);
        }

        protected void OnControllerDisconnected(EventArgs e)
        {
            if (ControllerDisconnected != null)
                ControllerDisconnected(this, e);
        }
        #endregion

        #region Constructors
        public GamePad(Microsoft.Xna.Framework.PlayerIndex playerIndex)
        {
            this.playerIndex = playerIndex;
            caps = new GamePadCapabilities();
            Update();
        }
        #endregion

        #region Properties
        public GamePadState State
        {
            get { return state; }
        }

        public GamePadDPad DPad
        {
            get { return state.DPad; }
        }

        public GamePadThumbSticks ThumbSticks
        {
            get { return state.ThumbSticks; }
        }

        public GamePadTriggers Triggers
        {
            get { return state.Triggers; }
        }

        public GamePadCapabilities Caps
        {
            get { return caps; }
        }
        public GamePadButtons Buttons
        {
            get { return state.Buttons; }
        }

        public bool IsConnected
        {
            get { return isconnected; }
            set { isconnected = value; }
        }
        #endregion

        #region Methods
        public void Update()
        {
            state = Pad.GetState(playerIndex);
            if (!isconnected && state.IsConnected)
                OnControllerConnected(EventArgs.Empty);

            if (isconnected && !state.IsConnected)
                OnControllerDisconnected(EventArgs.Empty);

            isconnected = state.IsConnected;

        }

        public void Vibrate(float leftSpeedy, float rightSpeedy)
        {
            Pad.SetVibration(playerIndex, leftSpeedy, rightSpeedy);
        }
        #endregion
    }
}
